package com.design.fitment.view;

/**
 * Created by 娇哥 on 2016/1/13.
 */

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.design.fitment.R;
import com.design.fitment.adapter.BannerListAdapter;
import com.design.fitment.adapter.JieXiListAdapter;
import com.design.fitment.entity.SheJiBen;
import com.design.fitment.util.DesignNetworkUtil;
import com.design.fitment.util.RequestDataCallback;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 头部组合控件
 */
public class MainHeaderView extends FrameLayout {
    @ViewInject(R.id.vp_ad_banner)
    private ViewPager mVpBanner;

    @ViewInject(R.id.ll_point)
    public LinearLayout mLLPoint;

    @ViewInject(R.id.rl_main_banner)
    public RelativeLayout mRlMainBanner;

    @ViewInject(R.id.lv_horizontal)
    private HorizontalListView mHorizontal;

    private BannerListAdapter mAdapter = null;
    private JieXiListAdapter mJieXiAdapter = null;

    private List<SheJiBen.DataEntity.BannerEntity> mBanners =
            new ArrayList<SheJiBen.DataEntity.BannerEntity>();
    private List<SheJiBen.DataEntity.JiexiEntity> mJieXis = new ArrayList<>();

    private static final String TAG = "print";
    private Context context;

    private int count;
    public MainHeaderView(Context context) {
        super(context);
        initViews();
        this.context = context;
    }


    private List<ImageView> mImageViews;
    private ImageView mCurrentImageView;
    private int currentPageNO = 0;
    private void initViews() {
        count = 1;
        View view =  LayoutInflater.from(getContext()).inflate(R.layout.frag_main_header_content, this, true);
        ViewUtils.inject(this, view);
        mImageViews = new ArrayList<ImageView>();
        mAdapter = new BannerListAdapter(getContext(),mBanners);
        mJieXiAdapter = new JieXiListAdapter(getContext(),mJieXis);

       /* // 获取屏幕显示尺寸对象
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // 动态设置ViewPager高度
        ViewGroup.LayoutParams layoutParams = mRlMainBanner.getLayoutParams();
        layoutParams.height = metrics.heightPixels / 3;
        mRlMainBanner.setLayoutParams(layoutParams)*/;
        mHorizontal.setAdapter(mJieXiAdapter);

        mVpBanner.setAdapter(mAdapter);
        mVpBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mCurrentImageView == null) {
                    return;
                }

                currentPageNO = position;

                mCurrentImageView.setSelected(false);
                mCurrentImageView = mImageViews.get(position);
                mCurrentImageView.setSelected(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    public  void loadBanner(){

        DesignNetworkUtil.getInstance().getBannaer(new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                SheJiBen banner = gson.fromJson(response, SheJiBen.class);
                if (banner != null) {
                    mBanners.addAll(banner.getData().getBanner());
                    mJieXis.addAll(banner.getData().getJiexi());
                    if (count ==1){
                        addPoints(context);
                        count ++;
                    }
                    timerChangeBanner();
                }

                mAdapter.notifyDataSetChanged();
                mJieXiAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException error) {
                Log.d("onFailure", "onFailure() returned: " + "error");
            }
        });
    }
    private void timerChangeBanner()
    {
        // mVpBanner选择对应页

        // 圆点切换

        mHander.sendEmptyMessageDelayed(1, 5000);
    }
    private Handler mHander = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            currentPageNO = ++currentPageNO % mImageViews.size();

            mCurrentImageView.setSelected(false);
            mCurrentImageView = mImageViews.get(currentPageNO);
            mCurrentImageView.setSelected(true);

            // mVpBanner选择对应页
            // 圆点切换
            mVpBanner.setCurrentItem(currentPageNO);

            mHander.sendEmptyMessageDelayed(1,5000);
        }
    };
    /**
     * 添加圆点
     */
    private void addPoints(Context context)
    {

        if (mBanners == null || mBanners.isEmpty())
        {
            return;
        }

        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);

        layoutParams.rightMargin = 10;

        int bannerSize = mBanners.size();
        for (int i = 0; i < bannerSize; i++)
        {
            mCurrentImageView = new ImageView(context);

            mCurrentImageView.setLayoutParams(layoutParams);

            //View view = new View(getActivity());
            //view.setSelected(true);

            mCurrentImageView.setSelected(false);

            mCurrentImageView.setImageResource(R.drawable.selector_nav_points);

            mImageViews.add(mCurrentImageView);

            mLLPoint.addView(mCurrentImageView,i);
        }

        mCurrentImageView = mImageViews.get(0);
        mCurrentImageView.setSelected(true);

    }




}
